﻿namespace Pub.Class
{
    using System;
    using System.Runtime.InteropServices;

    public class PagerSQLHelper : Disposable
    {
        private IPagerSQL PagerSQL;
        private Pub.Class.PagerSQLEnum PagerSQLEnum;

        public PagerSQLHelper(Pub.Class.PagerSQLEnum PagerSQLEnum)
        {
            this.PagerSQLEnum = PagerSQLEnum;
            this.init();
        }

        public PagerSQLHelper(string PagerSQLEnum)
        {
            this.PagerSQLEnum = PagerSQLEnum.ToEnum<Pub.Class.PagerSQLEnum>();
            this.init();
        }

        public PagerSql GetSQL(int pageIndex, int pageSize, string tableName, string pk = "*", string fieldList = "*", string where = "", string groupBy = "", string orderBy = "")
        {
            if ((pk.IsNullEmpty() && !fieldList.IsNullEmpty()) && (fieldList.IndexOf(",") > 0))
            {
                pk = fieldList.Split(new char[] { ',' })[0];
            }
            if ((pk.IsNullEmpty() && !fieldList.Equals("*")) && (fieldList.IndexOf(",") == -1))
            {
                pk = fieldList;
            }
            if (fieldList.IsNullEmpty())
            {
                fieldList = "*";
            }
            if (pk.IsNullEmpty())
            {
                pk = "*";
            }
            pk = pk.Trim(new char[] { ',' });
            fieldList = fieldList.Trim(new char[] { ',' });
            if ((orderBy.IsNullEmpty() && !pk.IsNullEmpty()) && !pk.Equals("*"))
            {
                orderBy = pk + " desc";
            }
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 10;
            }
            if (!where.IsNullEmpty())
            {
                where = "1=1 " + where;
            }
            return this.PagerSQL.GetSQL(pageIndex, pageSize, tableName, pk, fieldList, where, groupBy, orderBy);
        }

        private void init()
        {
            switch (this.PagerSQLEnum)
            {
                case Pub.Class.PagerSQLEnum.not_in:
                    this.PagerSQL = new NotInPagerSQL();
                    return;

                case Pub.Class.PagerSQLEnum.max_top:
                    this.PagerSQL = new MaxTopPagerSQL();
                    return;

                case Pub.Class.PagerSQLEnum.top_top:
                    this.PagerSQL = new TopTopPagerSQL();
                    return;

                case Pub.Class.PagerSQLEnum.row_number:
                    this.PagerSQL = new RowNumberPagerSQL();
                    return;

                case Pub.Class.PagerSQLEnum.mysql:
                    this.PagerSQL = new MySqlPagerSQL();
                    return;

                case Pub.Class.PagerSQLEnum.oracle:
                    this.PagerSQL = new OraclePagerSQL();
                    return;

                case Pub.Class.PagerSQLEnum.sqlite:
                    this.PagerSQL = new SqlitePagerSQL();
                    return;
            }
            this.PagerSQL = new MaxTopPagerSQL();
        }

        protected override void InternalDispose()
        {
            this.PagerSQL = null;
            base.InternalDispose();
        }
    }
}

